#include "stdafx.h"
#include "CommonFounction.h"


void GetExePath(std::wstring &wcsPath)
{
	static wstring s_wcsPath;
	static bool s_bInit = false;
	if(!s_bInit)
	{
		const int c_nBufSize = 512;
		wchar_t* pTemp = new wchar_t[c_nBufSize];
		::GetModuleFileNameW(NULL, pTemp, c_nBufSize);
		size_t i, nLen;
		nLen = ::wcslen(pTemp);		
		for(i = nLen - 1; i >= 0; i--)
		{
			if(L'\\' == pTemp[i]||L'/' == pTemp[i])
			{
				pTemp[i] = L'\0';
				s_wcsPath = pTemp;
				s_bInit = true;
				delete [] pTemp;
				break;
			}
		}
	}
	wcsPath = s_wcsPath;
}

void GetGamePath(std::wstring &wcsPath)
{
	static wstring s_wcsPath;
	static bool s_bInit = false;
	if(!s_bInit)
	{
		const int c_nBufSize = 512;
		wchar_t* pTemp = new wchar_t[c_nBufSize];
		::GetModuleFileNameW(NULL, pTemp, c_nBufSize);
		size_t i, nLen;
		nLen = ::wcslen(pTemp);
		int nNum = 0;
		for(i = nLen - 1; i >= 0; i--)
		{
			if(L'\\' == pTemp[i]||L'/' == pTemp[i])
			{
				nNum++;
				if(2 == nNum)
				{
					pTemp[i + 1] = L'\0';
					s_wcsPath = pTemp;
					s_bInit = true;
					delete [] pTemp;
					break;
				}
			}
		}
	}
	wcsPath = s_wcsPath + wcsPath;
}

void GetGamePath(std::string &csPath)
{
	USES_CONVERSION;
	std::wstring wcsPath = A2W(csPath.c_str());
	GetGamePath(wcsPath);
	if (!wcsPath.empty())
	{
		csPath = W2A(wcsPath.c_str());
	}
}

bool IsDirectoryExist(const std::wstring& dirName)
{
	bool rtn = false;

	WIN32_FIND_DATA FindFileData;
	HANDLE hFind;

	hFind = FindFirstFile(dirName.c_str(), &FindFileData);
	if (hFind != INVALID_HANDLE_VALUE) 
	{
		FindClose(hFind);
		rtn = true;
	}
	return rtn;
}

bool CreateDirectory(const std::wstring& dirName)
{
	//if (::CreateDirectory(dirName.c_str(),NULL))
	//{
	//	return true;
	//}
	//return false;
	size_t pos = dirName.find_first_of(L"/\\");
	if(wstring::npos == pos)
		return false;

	while(1)
	{
		CreateDirectoryW(dirName.substr(0,pos).c_str(),NULL);
		wstring path = dirName.substr(++pos);
		size_t tempPos = path.find_first_of(L"/\\");
		if(wstring::npos == tempPos)
			break;
		pos += tempPos;
	}
	return true;
}


HRESULT UnicodeToAnsi(const wchar_t* wcSrcText, char* szBuf, int &nBufSize)
{
	HRESULT hrRet = S_FALSE;
	if(NULL == wcSrcText)
	{
		goto RET_FALSE;
	}
	if(NULL == szBuf || 0 == nBufSize)
	{
		nBufSize = WideCharToMultiByte(CP_ACP, 0, wcSrcText, -1, szBuf, 0, NULL, NULL);
		goto RET_OK;
	}
	WideCharToMultiByte(CP_ACP, 0, wcSrcText, -1, szBuf, nBufSize, NULL, NULL);
RET_OK:
	hrRet = S_OK;
RET_FALSE:
	return hrRet;
}

HRESULT AnsiToUnicode(const char* szSrcText,  wchar_t* wcBuf, int &nBufSize)
{
	HRESULT hrRet = S_FALSE;
	if(NULL == szSrcText)
	{
		goto RET_FALSE;
	}
	if(NULL == wcBuf || 0 == nBufSize)
	{
		nBufSize = MultiByteToWideChar(CP_ACP, 0, szSrcText, -1, wcBuf, 0);
		goto RET_OK;
	}
	MultiByteToWideChar(CP_ACP, 0, szSrcText, -1, wcBuf, nBufSize);
RET_OK:
	hrRet = S_OK;
RET_FALSE:
	return hrRet;
}

HRESULT UnicodeToUtf8(const wchar_t* wcSrcText, char* szBuf, int &nBufSize)
{
	HRESULT hrRet = S_FALSE;
	if(NULL == wcSrcText)
	{
		goto RET_FALSE;
	}
	if(NULL == szBuf || 0 == nBufSize)
	{
		nBufSize = WideCharToMultiByte(CP_UTF8, 0, wcSrcText, -1, szBuf, 0, NULL, NULL);
		goto RET_OK;
	}
	WideCharToMultiByte(CP_UTF8, 0, wcSrcText, -1, szBuf, nBufSize, NULL, NULL);
RET_OK:
	hrRet = S_OK;
RET_FALSE:
	return hrRet;
}

HRESULT Utf8ToUnicode(const char* szSrcText,  wchar_t* wcBuf, int &nBufSize)
{
	HRESULT hrRet = S_FALSE;
	if(NULL == szSrcText)
	{
		goto RET_FALSE;
	}
	if(NULL == wcBuf || 0 == nBufSize)
	{
		nBufSize = MultiByteToWideChar(CP_UTF8, 0, szSrcText, -1, wcBuf, 0);
		goto RET_OK;
	}
	hrRet = MultiByteToWideChar(CP_UTF8, 0, szSrcText, -1, wcBuf, nBufSize);
RET_OK:
	hrRet = S_OK;
RET_FALSE:
	return hrRet;
}

namespace TempBuf
{
	const int c_g_nStrBufSize = 1024;
	char g_szStrBuf[c_g_nStrBufSize];
};

HRESULT AsniToUtf8(const char* szSrcText,  char* szBuf, int &nBufSize)
{
	HRESULT hrRet = S_FALSE;
	int nLen, nSize;
	wchar_t *pTemp = NULL;
	wchar_t *pTempTex = NULL;

	nLen = 0;
	hrRet = AnsiToUnicode(szSrcText, NULL, nLen);
	if(S_OK != hrRet)
	{
		goto RET_FALSE;
	}

	nSize = nLen * sizeof(wchar_t);

	if(nSize > TempBuf::c_g_nStrBufSize)
	{
		pTemp = new wchar_t[nLen];
		pTempTex = pTemp;
	}
	else
	{
		pTempTex = (wchar_t*)(TempBuf::g_szStrBuf);
	}
	hrRet = AnsiToUnicode(szSrcText, pTempTex, nLen);
	if(S_OK != hrRet)
	{
		goto RET_FALSE;
	}
	hrRet = UnicodeToUtf8(pTempTex, szBuf, nBufSize);
RET_FALSE:
	if(NULL != pTemp)
	{
		delete [] pTemp;
		pTemp = NULL;
	}
	return hrRet;
}

HRESULT Utf8ToAsni(const char* szSrcText,  char* szBuf, int &nBufSize)
{
	HRESULT hrRet = S_FALSE;
	int nLen, nSize;
	wchar_t *pTemp = NULL;
	wchar_t *pTempTex = NULL;

	nLen = 0;
	hrRet = Utf8ToUnicode(szSrcText, NULL, nLen);
	if(S_OK != hrRet)
	{
		goto RET_FALSE;
	}

	nSize = nLen * sizeof(wchar_t);

	if(nSize > TempBuf::c_g_nStrBufSize)
	{
		pTemp = new wchar_t[nLen];
		pTempTex = pTemp;
	}
	else
	{
		pTempTex = (wchar_t*)(TempBuf::g_szStrBuf);
	}
	hrRet = Utf8ToUnicode(szSrcText, pTempTex, nLen);
	if(S_OK != hrRet)
	{
		goto RET_FALSE;
	}
	hrRet = UnicodeToAnsi(pTempTex, szBuf, nBufSize);
RET_FALSE:
	if(NULL != pTemp)
	{
		delete [] pTemp;
		pTemp = NULL;
	}
	return hrRet;
}